package Application.MapApplication.view.ui
{
	import Application.MapApplication.def.Def;
	import Application.MapApplication.def.Manager;
	
	import XComponent.hide.XPage;
	
	import def.XG;
	
	import fl.containers.UILoader;
	import fl.controls.Button;
	import fl.controls.Label;
	
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class MapEditMapListMed extends UIBaseMed
	{
		public var view : Sprite;
		
		private var _closeBtn : SimpleButton;
		private var _preBtn : SimpleButton;
		private var _nextBtn : SimpleButton;
		private var _btnEIsoObj : Button;
		
		private var _panTitle : Sprite;
		
		private var _txtPage : Label;
		
		private var _imgLoadList : Array ;
		
		private var m_xpage : XPage;
		
		public function MapEditMapListMed()
		{
			super(Def.MapEditMapList,[Def.STAGE_ALIGN_MIDDLE]);
			
			view = this.viewComponent as Sprite;
		}
		
		override public function onRegister():void
		{
			_closeBtn = view["Btn_Close"];
			_preBtn = view["Btn_Pre"];
			_nextBtn = view["Btn_Next"];
			_btnEIsoObj = view["Btn_EIsoObj"];
			
			_panTitle = view["Pan_Title"];
			
			_txtPage = view["Txt_Page"];
			
			_imgLoadList = [];
			
			for(var i:int=0;i<12;i++)
			{
				_imgLoadList.push(view["UIL_Img"+i]);
				view["UIL_Img"+i].addEventListener(MouseEvent.CLICK,OnSelectImg);
			}
			i=0;
			Manager.XML.MapSrc.prase();
			
			m_xpage = new XPage(_preBtn,_nextBtn,Manager.XML.MapSrc.ArrayList("object").length);
			
			showPage(m_xpage.curPage);
			
			_closeBtn.addEventListener(MouseEvent.CLICK,OnClose);
			_panTitle.addEventListener(MouseEvent.MOUSE_DOWN,OnPanTitleMDown);
			_panTitle.addEventListener(MouseEvent.MOUSE_UP,OnPanTitleMUp);
			_btnEIsoObj.addEventListener(MouseEvent.CLICK,OnEIsoObj);
			
			view.addEventListener(MouseEvent.MOUSE_DOWN,OnMDown);
			
			m_xpage.addEventListener(XPage.PAGE_CHANGED,OnRecPageExchange);
			
			Manager.Mouse.startMove();
		}
		
		private function OnMDown(evt : MouseEvent) : void
		{
			XG.swapUI(this.getMediatorName());
		}
		
		private function OnRecPageExchange(evt : Event) : void
		{
			showPage(m_xpage.curPage);
		}
		
		private function setLabPage() : void
		{
			_txtPage.text = m_xpage.curPage +"/" + m_xpage.totalPage;
		}
		
		private function OnClose(evt : MouseEvent) : void
		{
			sendNotification(Def.MapEditMapList_Close);
		}
		
		private function OnPanTitleMDown(evt : MouseEvent) : void
		{
			view.alpha = 0.5;
			view.startDrag();
		}
		
		private function OnPanTitleMUp(evt : MouseEvent) : void
		{
			view.alpha = 1;
			view.stopDrag();
		}
		
		
		override public function listNotificationInterests() : Array
		{
			var notificatons : Array = super.listNotificationInterests();
			
			notificatons=notificatons.concat([Def.MapEditMapList_Show,Def.MapEditMapList_Close,Def.MapEditMapList_Turn,Def.STAGE_SIZE_CHANGED]);
			
			return notificatons;
		}
		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case Def.MapEditMapList_Show:
					XG.openUI(this.mediatorName);
					break;
				case Def.MapEditMapList_Close:
					destory();
					XG.closeUI(this.mediatorName);
					break;
				case Def.MapEditMapList_Turn : 
					XG.turnUI(this.mediatorName);
					break;
				default :
					break;
			}
			
			super.handleNotification(notification);
		}
		
		private function destory() : void
		{
			_closeBtn.removeEventListener(MouseEvent.CLICK,OnClose);
			_panTitle.removeEventListener(MouseEvent.MOUSE_DOWN,OnPanTitleMDown);
			_panTitle.removeEventListener(MouseEvent.MOUSE_UP,OnPanTitleMUp);
			_btnEIsoObj.addEventListener(MouseEvent.CLICK,OnEIsoObj);
			
			view.removeEventListener(MouseEvent.MOUSE_DOWN,OnMDown);
			
			m_xpage.removeEventListener(XPage.PAGE_CHANGED,OnRecPageExchange);
			
			facade.removeMediator(Def.MapEditMapList);
			facade.removeProxy(Def.MapEditMapList);
			
			m_xpage.destory();
			
			viewComponent = null;
			m_xpage = null;
		}
		
		private function OnEIsoObj(evt : MouseEvent) : void
		{
			sendNotification(Def.MapEdit_EditIsoObj,_btnEIsoObj.selected);
		}
		
		private function OnSelectImg(evt : MouseEvent) : void
		{
			var urlLoad : UILoader  = evt.currentTarget["UIL_Img"];
			var list : Array =  Manager.XML.MapSrc.ArrayList("object");
			var strPage : int = (m_xpage.curPage-1)*12;
			var endPage : int = strPage+12<list.length?strPage+12:list.length
			
			for (var i:int=strPage;i<strPage+12;i++)
			{
				var ind : int = (i)%12;
				
				if(evt.currentTarget == view["UIL_Img"+ind])
				{
					Manager.Mouse.loadHandBox.load(new URLRequest(list[i]["url"]));
					sendNotification(Def.MapEdit_SelectImgObjModel,list[i]);
				}
			}
		}
		
		private function  showPage(page : int) : void
		{
			var list : Array =  Manager.XML.MapSrc.ArrayList("object");
			var strPage : int = (page-1)*12;
			var endPage : int = strPage+12<list.length?strPage+12:list.length
			
			for (var i:int=strPage;i<strPage+12;i++)
			{
				var ind : int = (i)%12;
				
				var urlLoad : UILoader  = view["UIL_Img"+ind]["UIL_Img"];
				var imgNameTxt : Label = view["UIL_Img"+ind]["Txt_ImgName"];
				
				if(i < endPage)
				{
					urlLoad.load(new URLRequest(list[i]["url"]));
					imgNameTxt.text = list[i]["name"];
				}
				else
				{
					urlLoad.unload();
					imgNameTxt.text =  "";
				}
			}
			
			setLabPage();
		}
	}
}